package com.springboot.springbootnetty.nettyclientmudbus;


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;


public class NettyClient {
    private static final Logger logger = LoggerFactory.getLogger(NettyClient.class);

    public static void main(String[] args) throws InterruptedException {
        NettyClient nettyClient = new NettyClient();
        nettyClient.CreateNettyClient();
    }
    public void CreateNettyClient() throws InterruptedException {

        NioEventLoopGroup workEventLoopGroup = new NioEventLoopGroup();


        try {

            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(workEventLoopGroup)
                    .channel(NioSocketChannel.class)
                    .handler(

                            new ChannelInitializer<NioSocketChannel>() {
                                @Override
                                protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                                    ChannelPipeline pipeline = nioSocketChannel.pipeline();
                                    // 添加编码器   暂时不加


                                    // 添加自定义处理器
                                    pipeline.addLast(new MyNettyClientHandler());

                                }
                            }
                    );

            ChannelFuture channelFuture = bootstrap
                    .connect("127.0.0.1", 6669)
                    .sync();


            Channel channel = channelFuture.channel();

//            System.out.println("请输入要发送Mudbus信息逗号分隔:");
//
//            Scanner scanner = new Scanner(System.in);
//
//            while (scanner.hasNextLine()) {
//
//                String s = scanner.nextLine();
//
//                String[] StringSplit = s.trim().split(",");
//
//                byte convest = MainWork.convest(StringSplit[1]);
//
//                if (convest == 0xff) {
//                    continue;
//                }
//                // 组织要发送的Mudbus 信息
//                ModbusMessage message = MainWork.loadmessage(StringSplit, convest);
//
//                if (StringSplit[0] == ("1")) {
//                    // 向服务器发送消息
//                    channel.writeAndFlush(message);
//
//                } else {
//                    channel.eventLoop().scheduleAtFixedRate(new Runnable() {
//                        @Override
//                        public void run() {
//                            channel.writeAndFlush(message);
//                        }
//                    }, 1, 5, TimeUnit.SECONDS);
//                }
//            }
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture channelFuture) throws Exception {
                    if (channelFuture.isSuccess()) {
                        logger.info("客户端启动成功");
                    } else {
                        logger.info("客户端启动失败");
                    }
                }
            });
            //对通道关闭进行监听
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            logger.info("客户端启动失败:{}", e.getMessage());
            throw new RuntimeException(e);
        } finally {
            // 关闭线程组
            workEventLoopGroup.shutdownGracefully();
        }

    }


}
